﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common.DBOper;
using Common;

namespace VibrationTest
{
    public partial class frmChannelSet : Form
    {
        public frmChannelSet()
        {
            InitializeComponent();
        }
        public int ChannelCount = 64;
        OperateTest opertest = new OperateTest();
        OperateChannel operchannel = new OperateChannel();
        Cls_SocketSet socketset = new Cls_SocketSet();
        public Dictionary<string, byte> DirectionToByte = new Dictionary<string, byte>();
        public Dictionary<string, byte> SigTypeToByte = new Dictionary<string, byte>();
        public Dictionary<string, byte> SigUnitToByte = new Dictionary<string, byte>();
        public Dictionary<string, byte> SensitivityUnitToByte = new Dictionary<string, byte>();

        public Dictionary<byte, string> DirectionToString = new Dictionary<byte, string>();
        public Dictionary<byte, string> SigTypeToString = new Dictionary<byte, string>();
        public Dictionary<byte, string> SigUnitToString = new Dictionary<byte, string>();
        public Dictionary<byte, string> SensitivityUnitToString = new Dictionary<byte, string>();

        public Dictionary<string,Cls_SigSet> sigsetlist = new Dictionary<string,Cls_SigSet>();
        OperateSigSet operateSigSet = new OperateSigSet();
        private void frmChannelSet_Load(object sender, EventArgs e)
        {
            Program.formchannelset = this;

            LoadChannelSet();
     
        }


        public void LoadChannelSet()
        {
            sigsetlist.Clear();
           List<Cls_SigSet>  sigsetlistnew = operateSigSet.GetSigSetList();
            if (sigsetlistnew!=null&& sigsetlistnew.Count>0) {
                for (int i = 0; i < sigsetlistnew.Count; i++)
                {
                    if (!sigsetlist.ContainsKey(sigsetlistnew[i].SigName))
                    {
                        sigsetlist.Add(sigsetlistnew[i].SigName, sigsetlistnew[i]);
                    }
                }
            }
            DirectionToByte.Clear();
            SigTypeToByte.Clear();
            SigUnitToByte.Clear();
            SensitivityUnitToByte.Clear();
            DirectionToString.Clear();
            SigTypeToString.Clear();
            SigUnitToString.Clear();
            SensitivityUnitToString.Clear();
            if (Mdl_Class.NowStectionId > 0)
            {
                Cls_Scoket socket = socketset.GetSocket();
                ChannelCount = socket.ChannelCount;
            }
            GetDirection();
            GetSigType();
            GetSigUnit();
            GetSensitivityUnit();
            DisplaySet();
          

        }



        public void DisplaySet() 
        {
            this.dataGridView1.Rows.Clear();
            if (Mdl_Class.NowStectionId > 0)
            {
                List<Common.DBOper.Cls_Channel> clschannellist = operchannel.GetChannelList(Mdl_Class.NowStectionId);
                Dictionary<int, Common.DBOper.Cls_Channel> channelidtochannel = new Dictionary<int, Common.DBOper.Cls_Channel>();
                for (int i = 0; i < clschannellist.Count; i++)
                {
                    channelidtochannel.Add(clschannellist[i].ChannelId,clschannellist[i]);
                }
                for (int i = 0; i < ChannelCount; i++)
                {
                    if (channelidtochannel.ContainsKey((i + 1)))
                    {
                        this.dataGridView1.Rows.Add((i + 1).ToString(), "Input" + (i + 1).ToString(), channelidtochannel[i+1].IsUse==1?true:false,
                            channelidtochannel[i + 1].TestPointName,
                            DirectionToString[channelidtochannel[i + 1].Direction],
                            SigTypeToString[channelidtochannel[i + 1].SigType],
                            SigUnitToString[channelidtochannel[i + 1].SigUnit],
                           channelidtochannel[i + 1].Sensitivity.ToString(), 
                            null,
                           channelidtochannel[i + 1].SensorType,
                           channelidtochannel[i + 1].SensorManufacturer,
                           channelidtochannel[i + 1].SensorNo);
                        BindSensitivityUnit(channelidtochannel[i + 1].SigType, i);
                        this.dataGridView1.Rows[i].Cells["SensitivityUnit"].Value = SensitivityUnitToString[channelidtochannel[i + 1].SensitivityUnit];

                        BindSerSensorType(i);
                        if (sigsetlist.ContainsKey(channelidtochannel[i + 1].SensorType)) {
                            this.dataGridView1.Rows[i].Cells["SensorType"].Value = sigsetlist[channelidtochannel[i + 1].SensorType].SigName;
                        }
                        else
                        {
                            this.dataGridView1.Rows[i].Cells["SensorType"].Value ="";
                        }
                    }
                    else 
                    {

                        this.dataGridView1.Rows.Add((i + 1).ToString(), "Input" + (i + 1).ToString(), false, "point" + (i + 1).ToString(),
                            DirectionToString[0], SigTypeToString[0], SigUnitToString[0], "100.0", null, "", "", "");
                        BindSensitivityUnit(0, i);
                        BindSerSensorType(i);
                        this.dataGridView1.Rows[i].Cells["SensitivityUnit"].Value = SensitivityUnitToString[0];
                    }
                }
            }
            //else 
            //{

            //    this.dataGridView1.Rows.Clear();


            //    for (int i = 0; i < ChannelCount; i++)
            //    {

                  
            //        this.dataGridView1.Rows.Add((i + 1).ToString(), "Input" + (i + 1).ToString(), false, "point" + (i + 1).ToString(),
            //            DirectionToString[0], SigTypeToString[0], SigUnitToString[0], "0.0", null, "", "", "");
            //        BindSensitivityUnit(0, i);
            //        this.dataGridView1.Rows[i].Cells["SensitivityUnit"].Value = SensitivityUnitToString[0];
                    
            //    }
            //}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveChannelSet();
        }

        public void SaveChannelSet() 
        {
            if (Mdl_Class.IsQuitProgram==false) 
            {
                MessageBox.Show("数据采集中，不能修改参数设置","提示");
                return;
            }
            List<Common.DBOper.Cls_Channel> channellist = new List<Common.DBOper.Cls_Channel>();
            for (int i = 0; i < dataGridView1.Rows.Count;i++ )
            {
               Common.DBOper.Cls_Channel clschannel = new Common.DBOper.Cls_Channel();
               clschannel.SectionId = Mdl_Class.NowStectionId;
                clschannel.ChannelId = Convert.ToByte(dataGridView1.Rows[i].Cells["channelId"].Value);
                clschannel.ChannelName = dataGridView1.Rows[i].Cells["ChannelName"].Value.ToString();
                clschannel.IsUse = dataGridView1.Rows[i].Cells["IsUse"].Value.ToString().ToUpper() == "TRUE" ? (byte)1 :(byte) 0;
                clschannel.TestPointName = dataGridView1.Rows[i].Cells["TestPointName"].Value.ToString();
                clschannel.Direction = DirectionToByte[dataGridView1.Rows[i].Cells["Direction"].Value.ToString()];
                clschannel.SigType = SigTypeToByte[dataGridView1.Rows[i].Cells["SigType"].Value.ToString()];
                clschannel.SigUnit = SigUnitToByte[dataGridView1.Rows[i].Cells["SigUnit"].Value.ToString()];
                clschannel.Sensitivity = Convert.ToSingle(dataGridView1.Rows[i].Cells["Sensitivity"].Value);
                clschannel.SensitivityUnit = SensitivityUnitToByte[dataGridView1.Rows[i].Cells["SensitivityUnit"].Value.ToString()];
                clschannel.SensorType = dataGridView1.Rows[i].Cells["SensorType"].Value.ToString();
                clschannel.SensorManufacturer = dataGridView1.Rows[i].Cells["SensorManufacturer"].Value.ToString();
                clschannel.SensorNo = dataGridView1.Rows[i].Cells["SensorNO"].Value.ToString();
                channellist.Add(clschannel);
            }

            if (operchannel.AddCls_Channel(channellist, Mdl_Class.NowStectionId) >= 0)
            {
                MessageBox.Show("保存成功", "提示");
                if (Program.formRun != null) 
                {
                    Program.formRun.LoadChannel();
                }
                if (Program.formTest != null)
                {
                    Program.formTest.LoadChannel();
                }
            }
            else 
            {
                MessageBox.Show("保存失败", "提示");
            }
        }

        public void GetDirection() 
        {
            string DirectionTxT = "None";
            byte DirectionIndex = 0;
            DirectionToByte.Add(DirectionTxT, DirectionIndex);
            DirectionToString.Add(DirectionIndex, DirectionTxT);
            DirectionIndex = 1;
            DirectionTxT = "+X";
            DirectionToByte.Add(DirectionTxT, DirectionIndex);
            DirectionToString.Add(DirectionIndex, DirectionTxT);
            DirectionIndex = 2;
            DirectionTxT = "-X";
            DirectionToByte.Add(DirectionTxT, DirectionIndex);
            DirectionToString.Add(DirectionIndex, DirectionTxT);
            DirectionIndex =3;
            DirectionTxT = "+Y";
            DirectionToByte.Add(DirectionTxT, DirectionIndex);
            DirectionToString.Add(DirectionIndex, DirectionTxT);
            DirectionIndex = 4;
            DirectionTxT = "-Y";
            DirectionToByte.Add(DirectionTxT, DirectionIndex);
            DirectionToString.Add(DirectionIndex, DirectionTxT);
            DirectionIndex = 5;
            DirectionTxT = "+Z";
            DirectionToByte.Add(DirectionTxT, DirectionIndex);
            DirectionToString.Add(DirectionIndex, DirectionTxT);
            DirectionIndex = 6;
            DirectionTxT = "-Z";
            DirectionToByte.Add(DirectionTxT, DirectionIndex);
            DirectionToString.Add(DirectionIndex, DirectionTxT);
            DirectionIndex = 7;
            DirectionTxT = "+RX";
            DirectionToByte.Add(DirectionTxT, DirectionIndex);
            DirectionToString.Add(DirectionIndex, DirectionTxT);
            DirectionIndex = 8;
            DirectionTxT = "-RX";
            DirectionToByte.Add(DirectionTxT, DirectionIndex);
            DirectionToString.Add(DirectionIndex, DirectionTxT);
            DirectionIndex = 9;
            DirectionTxT = "+RY";
            DirectionToByte.Add(DirectionTxT, DirectionIndex);
            DirectionToString.Add(DirectionIndex, DirectionTxT);
            DirectionIndex = 10;
            DirectionTxT = "-RY";
            DirectionToByte.Add(DirectionTxT, DirectionIndex);
            DirectionToString.Add(DirectionIndex, DirectionTxT);
            DirectionIndex = 11;
            DirectionTxT = "+RZ";
            DirectionToByte.Add(DirectionTxT, DirectionIndex);
            DirectionToString.Add(DirectionIndex, DirectionTxT);
            DirectionIndex = 12;
            DirectionTxT = "-RZ";
            DirectionToByte.Add(DirectionTxT, DirectionIndex);
            DirectionToString.Add(DirectionIndex, DirectionTxT);
            DirectionIndex = 13;
            DirectionTxT = "S";
            DirectionToByte.Add(DirectionTxT, DirectionIndex);
            DirectionToString.Add(DirectionIndex, DirectionTxT);
        }

    
        public void GetSigType()
        {

            string DirectionTxT = "加速度";
            byte DirectionIndex = 0;
            SigTypeToByte.Add(DirectionTxT, DirectionIndex);
            SigTypeToString.Add(DirectionIndex, DirectionTxT);

            DirectionTxT = "速度";
            DirectionIndex = 1;
            SigTypeToByte.Add(DirectionTxT, DirectionIndex);
            SigTypeToString.Add(DirectionIndex, DirectionTxT);

            DirectionTxT = "位移";
            DirectionIndex = 2;
            SigTypeToByte.Add(DirectionTxT, DirectionIndex);
            SigTypeToString.Add(DirectionIndex, DirectionTxT);

            DirectionTxT = "声压";
            DirectionIndex = 3;
            SigTypeToByte.Add(DirectionTxT, DirectionIndex);
            SigTypeToString.Add(DirectionIndex, DirectionTxT);

        }


        public void GetSigUnit()
        {

            string DirectionTxT = "mV";
            byte DirectionIndex = 0;
            SigUnitToByte.Add(DirectionTxT, DirectionIndex);
            SigUnitToString.Add(DirectionIndex, DirectionTxT);

             DirectionTxT = "V";
             DirectionIndex = 1;
            SigUnitToByte.Add(DirectionTxT, DirectionIndex);
            SigUnitToString.Add(DirectionIndex, DirectionTxT);

        }





        public void GetSensitivityUnit()
        {
            string DirectionTxT = "mV/g";
            byte DirectionIndex = 0;
            SensitivityUnitToByte.Add(DirectionTxT, DirectionIndex);
            SensitivityUnitToString.Add(DirectionIndex, DirectionTxT);


            DirectionTxT = "mV/（m/s^2）";
             DirectionIndex = 1;
            SensitivityUnitToByte.Add(DirectionTxT, DirectionIndex);
            SensitivityUnitToString.Add(DirectionIndex, DirectionTxT);

            DirectionTxT = "mV/（in/s^2）";
             DirectionIndex = 2;
            SensitivityUnitToByte.Add(DirectionTxT, DirectionIndex);
            SensitivityUnitToString.Add(DirectionIndex, DirectionTxT);


            DirectionTxT = "mV/（m/s）";
            DirectionIndex = 3;
            SensitivityUnitToByte.Add(DirectionTxT, DirectionIndex);
            SensitivityUnitToString.Add(DirectionIndex, DirectionTxT);


            DirectionTxT = "mV/（in/s）";
            DirectionIndex = 4;
            SensitivityUnitToByte.Add(DirectionTxT, DirectionIndex);
            SensitivityUnitToString.Add(DirectionIndex, DirectionTxT);


            DirectionTxT = "mV/mm";
            DirectionIndex = 5;
            SensitivityUnitToByte.Add(DirectionTxT, DirectionIndex);
            SensitivityUnitToString.Add(DirectionIndex, DirectionTxT);


            DirectionTxT = "mV/in";
            DirectionIndex = 6;
            SensitivityUnitToByte.Add(DirectionTxT, DirectionIndex);
            SensitivityUnitToString.Add(DirectionIndex, DirectionTxT);


            DirectionTxT = "mv/Pa";
            DirectionIndex = 7;
            SensitivityUnitToByte.Add(DirectionTxT, DirectionIndex);
            SensitivityUnitToString.Add(DirectionIndex, DirectionTxT);


            DirectionTxT = "mv/psi";
            DirectionIndex =8;
            SensitivityUnitToByte.Add(DirectionTxT, DirectionIndex);
            SensitivityUnitToString.Add(DirectionIndex, DirectionTxT);


            DirectionTxT = "mv/bar";
            DirectionIndex = 9;
            SensitivityUnitToByte.Add(DirectionTxT, DirectionIndex);
            SensitivityUnitToString.Add(DirectionIndex, DirectionTxT);

        }



        public void BindSensitivityUnit(byte GetSigType,int rowindex) 
        {
           DataTable dtVersion  =   new  DataTable();
           DataColumn dcVersion = new DataColumn("SensitivityUnitID",Type.GetType("System.String")); // 显示Name
           DataColumn dcVerGUID = new DataColumn("SensitivityUnitText",Type.GetType("System.String")); // 绑定的Value
         dtVersion.Columns.Add(dcVersion);
         dtVersion.Columns.Add(dcVerGUID);


    
            switch (GetSigType)
	    {
             case 0:
                dtVersion.Rows.Add(new object[] { "mV/g", "mV/g" });
                dtVersion.Rows.Add(new object[] { "mV/（m/s^2）", "mV/（m/s^2）" });
                dtVersion.Rows.Add(new object[] { "mV/（in/s^2）", "mV/（in/s^2）" });
                 break;
             case 1:
                 dtVersion.Rows.Add(new object[] { "mV/（m/s）", "mV/（m/s）" });
                 dtVersion.Rows.Add(new object[] { "mV/（in/s）", "mV/（in/s）" });
                 break;
             case 2:
                 dtVersion.Rows.Add(new object[] { "mV/mm", "mV/mm" });
                 dtVersion.Rows.Add(new object[] { "mV/in", "mV/in" });
                 break;
             case 3:
                 dtVersion.Rows.Add(new object[] { "mv/Pa", "mv/Pa" });
                 dtVersion.Rows.Add(new object[] { "mv/psi", "mv/psi" });
                 dtVersion.Rows.Add(new object[] { "mv/bar", "mv/bar" });
                 break;
		default:
                 break;
	    }
          
            ((DataGridViewComboBoxCell)this.dataGridView1.Rows[rowindex].Cells["SensitivityUnit"]).DataSource = dtVersion;
         ((DataGridViewComboBoxCell)this.dataGridView1.Rows[rowindex].Cells["SensitivityUnit"]).ValueMember = "SensitivityUnitID";
         ((DataGridViewComboBoxCell)this.dataGridView1.Rows[rowindex].Cells["SensitivityUnit"]).DisplayMember = "SensitivityUnitText";



        }


        public void BindSerSensorType(int rowindex)
        {
            DataTable dtSig = new DataTable();
            DataColumn dtSig_Name = new DataColumn("SigName", Type.GetType("System.String")); // 显示Name
            DataColumn dtSig_Value = new DataColumn("SigValue", Type.GetType("System.String")); // 绑定的Value
            dtSig.Columns.Add(dtSig_Name);
            dtSig.Columns.Add(dtSig_Value);
            if (sigsetlist != null && sigsetlist.Count > 0)
            {

                foreach (var item in sigsetlist.Keys)
                {
                    dtSig.Rows.Add(new object[] { item, item });
                }
            }

         ((DataGridViewComboBoxCell)this.dataGridView1.Rows[rowindex].Cells["SensorType"]).DataSource = dtSig;
            ((DataGridViewComboBoxCell)this.dataGridView1.Rows[rowindex].Cells["SensorType"]).ValueMember = "SigName";
            ((DataGridViewComboBoxCell)this.dataGridView1.Rows[rowindex].Cells["SensorType"]).DisplayMember = "SigValue";
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            try
            {

      
            if (e.ColumnIndex == 5 && e.RowIndex > 0)
            {
                byte a = SigTypeToByte[dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString()];
                BindSensitivityUnit(a, e.RowIndex);
            }

            if (e.ColumnIndex == 2 && e.RowIndex >= 0)
            {

                AutoSave();
                this.dataGridView1.Refresh();
            }
            }
            catch (Exception)
            {

             
            }
            //if (e.ColumnIndex == 9 && e.RowIndex > 0)
            //{
            //    string sigtype = dataGridView1.Rows[e.RowIndex].Cells[9].Value.ToString();
            //    if(sigsetlist.ContainsKey(sigtype))
            //    {
            //        this.dataGridView1.Rows[e.RowIndex].Cells["SigType"].Value = sigsetlist[sigtype].SigType;
            //        this.dataGridView1.Rows[e.RowIndex].Cells["SigUnit"].Value = sigsetlist[sigtype].SigUnit;
            //        this.dataGridView1.Rows[e.RowIndex].Cells["Sensitivity"].Value = sigsetlist[sigtype].Sensitivity;
            //        this.dataGridView1.Rows[e.RowIndex].Cells["SensitivityUnit"].Value = sigsetlist[sigtype].SensitivityUnit;
            //    }
            //}
        }
        int nowbyte = 0;
        private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            try
            {

   
            if (e.ColumnIndex == 5 && e.RowIndex >= 0)
            {
                byte a = SigTypeToByte[dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString()];
                if (nowbyte==a)
                {
                    return;
                }
                nowbyte = a;
                BindSensitivityUnit(a, e.RowIndex);
              
                
                switch (a)
                {
                    case 0:
                        this.dataGridView1.Rows[e.RowIndex].Cells["SensitivityUnit"].Value = SensitivityUnitToString[0];
                        break;
                    case 1:
                        this.dataGridView1.Rows[e.RowIndex].Cells["SensitivityUnit"].Value = SensitivityUnitToString[3];
                        break;
                    case 2:
                        this.dataGridView1.Rows[e.RowIndex].Cells["SensitivityUnit"].Value = SensitivityUnitToString[5];
                        break;
                    case 3:
                        this.dataGridView1.Rows[e.RowIndex].Cells["SensitivityUnit"].Value = SensitivityUnitToString[7];
                        break;
                    default:
                        break;
                }
            }

           
            AutoSave();
            //this.dataGridView1.Refresh();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void AutoSave()
        {
            if (Mdl_Class.IsQuitProgram == false)
            {
                MessageBox.Show("数据采集中，不能修改参数设置", "提示");
                return;
            }
            List<Common.DBOper.Cls_Channel> channellist = new List<Common.DBOper.Cls_Channel>();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                Common.DBOper.Cls_Channel clschannel = new Common.DBOper.Cls_Channel();
                clschannel.SectionId = Mdl_Class.NowStectionId;
                clschannel.ChannelId = Convert.ToByte(dataGridView1.Rows[i].Cells["channelId"].Value);
                clschannel.ChannelName = dataGridView1.Rows[i].Cells["ChannelName"].Value.ToString();
                clschannel.IsUse = dataGridView1.Rows[i].Cells["IsUse"].Value.ToString().ToUpper() == "TRUE" ? (byte)1 : (byte)0;
                clschannel.TestPointName = dataGridView1.Rows[i].Cells["TestPointName"].Value.ToString();
                clschannel.Direction = DirectionToByte[dataGridView1.Rows[i].Cells["Direction"].Value.ToString()];
                clschannel.SigType = SigTypeToByte[dataGridView1.Rows[i].Cells["SigType"].Value.ToString()];
                clschannel.SigUnit = SigUnitToByte[dataGridView1.Rows[i].Cells["SigUnit"].Value.ToString()];
                clschannel.Sensitivity = Convert.ToSingle(dataGridView1.Rows[i].Cells["Sensitivity"].Value);
                clschannel.SensitivityUnit = SensitivityUnitToByte[dataGridView1.Rows[i].Cells["SensitivityUnit"].Value.ToString()];
                clschannel.SensorType = dataGridView1.Rows[i].Cells["SensorType"].Value.ToString();
                clschannel.SensorManufacturer = dataGridView1.Rows[i].Cells["SensorManufacturer"].Value.ToString();
                clschannel.SensorNo = dataGridView1.Rows[i].Cells["SensorNO"].Value.ToString();
                channellist.Add(clschannel);
            }

            if (operchannel.AddCls_Channel(channellist, Mdl_Class.NowStectionId) >= 0)
            {
                if (Program.formRun != null)
                {
                    Program.formRun.LoadChannel();
                }
                if (Program.formTest != null)
                {
                    Program.formTest.LoadChannel();
                }
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {

       
            nowColumIndex = e.ColumnIndex;
            nowRowIndex = e.RowIndex;
            if (e.ColumnIndex == 5 && e.RowIndex >= 0)
            {
                nowbyte = SigTypeToByte[dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString()];
            }
            }
            catch (Exception)
            {

       
            }
            //if (e.ColumnIndex == 2 && e.RowIndex >= 0)
            //{
            //    AutoSave();
            //    this.dataGridView1.Refresh();
            //}

        }
        public DataGridViewTextBoxEditingControl CellEdit = null;
        public ComboBox cb_SensorType = new ComboBox();
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            try
            {

     
            if (this.dataGridView1.CurrentCellAddress.X == 11)//获取当前处于活动状态的单元格索引
            {
                CellEdit = (DataGridViewTextBoxEditingControl)e.Control;
                CellEdit.SelectAll();
                CellEdit.KeyPress += Cells_KeyPress; //绑定事件
            }

            if (dataGridView1.CurrentCell.OwningColumn.Name== "SensorType" && dataGridView1.CurrentCell.RowIndex>=0)
            {
                (e.Control as ComboBox).SelectedIndexChanged += new EventHandler(Cb_SensorType_SelectedIndexChanged);
            }

            if (dataGridView1.CurrentCell.OwningColumn.Name == "IsUse" && dataGridView1.CurrentCell.RowIndex >= 0)
            {
                (e.Control as CheckBox).CheckedChanged += new EventHandler(FrmChannelSet_CheckedChanged);
            }
            }
            catch (Exception)
            {

              
            }
        }

        private void FrmChannelSet_CheckedChanged(object sender, EventArgs e)
        {
            AutoSave();
            this.dataGridView1.Refresh();
        }

        public int nowColumIndex = 0;
        public int nowRowIndex = 0;
        private void Cb_SensorType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

        
            if (nowColumIndex == 9 && nowRowIndex >= 0)
            {
                if ((sender as ComboBox).SelectedValue==null)
                {
                    return;
                }
                string sigtype = (sender as ComboBox).SelectedValue.ToString();
                this.dataGridView1.Rows[nowRowIndex].Cells["SensorType"].Value = sigtype;
                if (sigsetlist.ContainsKey(sigtype))
                {
                    this.dataGridView1.Rows[nowRowIndex].Cells["SigType"].Value = sigsetlist[sigtype].SigType;
                    this.dataGridView1.Rows[nowRowIndex].Cells["SigUnit"].Value = sigsetlist[sigtype].SigUnit;
                    this.dataGridView1.Rows[nowRowIndex].Cells["Sensitivity"].Value = sigsetlist[sigtype].Sensitivity;
                    this.dataGridView1.Rows[nowRowIndex].Cells["SensitivityUnit"].Value = sigsetlist[sigtype].SensitivityUnit;

                    byte a = SigTypeToByte[dataGridView1.Rows[nowRowIndex].Cells[5].Value.ToString()];
                    //if (nowbyte == a)
                    //{
                    //    return;
                    //}
                    nowbyte = a;
                    BindSensitivityUnit(a, nowRowIndex);


                    switch (a)
                    {
                        case 0:
                            this.dataGridView1.Rows[nowRowIndex].Cells["SensitivityUnit"].Value = SensitivityUnitToString[0];
                            break;
                        case 1:
                            this.dataGridView1.Rows[nowRowIndex].Cells["SensitivityUnit"].Value = SensitivityUnitToString[3];
                            break;
                        case 2:
                            this.dataGridView1.Rows[nowRowIndex].Cells["SensitivityUnit"].Value = SensitivityUnitToString[5];
                            break;
                        case 3:
                            this.dataGridView1.Rows[nowRowIndex].Cells["SensitivityUnit"].Value = SensitivityUnitToString[7];
                            break;
                        default:
                            break;
                    }

                    AutoSave();
                    //this.dataGridView1.Refresh();
                }
            }
            }
            catch (Exception)
            {

             
            }

        }

        private void Cells_KeyPress(object sender, KeyPressEventArgs e) //自定义事件
        {
            if (this.dataGridView1.CurrentCellAddress.X == 11) //获取当前处于活动状态的单元格索引
            {
                if (!(e.KeyChar >= '0' && e.KeyChar <= '9')) e.Handled = true;
                if (e.KeyChar == '\b') e.Handled = false;
            }
        }

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            Keys key = e.KeyCode;

            //Delete删除内容
            //if (key == Keys.Delete)
            //{
            //    TextDaseOperationClass.dgvDel(dataGridView1);
            //}

            if (e.Control != true)//如果没按Ctrl键
                return;
            switch (key)
            {
                case Keys.C:
                    //按下Ctrl+C复制
                    dgvCopy(dataGridView1);
                    break;

                case Keys.V:
                    //按下Ctrl+V粘贴
                    dgvPaste(dataGridView1);
                    break;

                    //case Keys.X:
                    //    //按下Ctrl+V剪切
                    //    TextDaseOperationClass.dgvCut(dataGridView1);
                    //    break;
            }
        }


        public void dgvCopy(DataGridView dgv)
        {
            if (dgv.GetCellCount(DataGridViewElementStates.Selected) > 0)
            {
                try
                {
                    Clipboard.SetDataObject(dgv.GetClipboardContent());
                }
                catch (Exception MyEx)
                {
                    MessageBox.Show(MyEx.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }


        public void dgvPaste(DataGridView dgv)
        {
            try
            {
                //最后一行为新行
                int rowCount = dgv.Rows.Count - 1;
                int colCount = dgv.ColumnCount;
                //获取剪贴板内容
                string pasteText = Clipboard.GetText();
                //判断是否有字符存在
                if (string.IsNullOrEmpty(pasteText))
                    return;
                //以换行符分割的数组
                string[] lines = pasteText.Trim().Split('\n');
                int txtLength = lines.Length;

                //Lance.2015-12-03
                for (int m=0;m< dgv.SelectedCells.Count;m++ ) {

                    int cRowIndex = dgv.SelectedCells[m].RowIndex;
                    int cColIndex = dgv.SelectedCells[m].ColumnIndex;
                    for (int i = 0; i < txtLength; i++)
                    {
                        string[] words = lines[i].Split('\t');
                        for (int j = 0; j < words.Length; j++)
                        {
                            dgv.Rows[cRowIndex + i].Cells[cColIndex + j].Value = words[j];
                        }
                    }
                }

            }
            catch (Exception MyEx)
            {
                MessageBox.Show(MyEx.Message);
            }
        }

        private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2 && e.RowIndex >= 0)
            {

                AutoSave();
                this.dataGridView1.Refresh();
            }
        }

        private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            try
            {


            if (dataGridView1.IsCurrentCellDirty)
            {
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
            }
            catch (Exception)
            {

         
            }
        }

        private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
